package
{
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.TimerEvent;
	import flash.media.SoundTransform;
	import flash.net.NetStream;
	import flash.net.NetStreamInfo;
	import flash.utils.Timer;
	
	import mx.utils.ObjectUtil;
	
	public class NetStream2
	{
		private var ns:NetStream;
		private var nc:NetConnection2;
		private var name:String;
		public var dispatcher:EventDispatcher = new EventDispatcher(null);
		
		public static const PLAY_EVENT:String = "PLAY_EVENT";
		public static const STOP_EVENT:String = "STOP_EVENT";
		public static const PUBLISH_EVENT:String = "PUBLISH_EVENT";
		public static const UNPUBLISH_EVENT:String = "UNPUBLISH_EVENT";
		public static const UNKNOWN_EVENT:String = "UNKNOWN_EVENT";
		
		[Bindable]
		public var volume:uint = 100;		
		
		[Bindable]
		private  var bufor:Number = 0;		

		public function NetStream2(nc:NetConnection2, time:int)
		{
			this.nc = nc;			
		}
		
		public function play(name:String):void {			
			ns = new NetStream(nc.nc);
			ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusEventHandler);
			ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, netAsync);
			ns.addEventListener(IOErrorEvent.IO_ERROR, netIO);
			this.name = name;
			ns.play(name);
			
			ns.client = new NetStreamClientWowza();
			
			setVolume(volume);
			setBufor(bufor);
			
		}
		

		public function netAsync(e:AsyncErrorEvent):void {
			trace(ObjectUtil.toString(e));
		}
		
		public function netIO(e:IOErrorEvent):void {
			trace(ObjectUtil.toString(e));
		}
		
		public function netStatusEventHandler(e:NetStatusEvent):void {
			//trace(e.info.code);
			switch (e.info.code) {
				case "NetStream.Play.PublishNotify": { 
					dispatcher.dispatchEvent(new Event(PUBLISH_EVENT)); 
					break;
				}
				case "NetStream.Play.UnpublishNotify": { 
					dispatcher.dispatchEvent(new Event(UNPUBLISH_EVENT)); 
					break;
				}
				case "NetStream.Play.Start": { 
					dispatcher.dispatchEvent(new Event(PLAY_EVENT)); 
					break;
				}
				case "NetStream.Play.Stop": { 
					dispatcher.dispatchEvent(new Event(STOP_EVENT)); 
					break;
				}
				case "NetStream.Play.Reset": {
					break;
				}
				case "NetStream.Buffer.Full": {
					break;
				}
				default: {
					dispatcher.dispatchEvent(new Event(UNKNOWN_EVENT));
					trace("UNKNOWN_EVENT: " + e.info.code);
					break;
				}
					
					
			}		
			
		}
		
		public function setVolume(inp:int):void {
			volume = inp;
			if (ns) {
				var snd:SoundTransform = new SoundTransform(inp/100);			
				ns.soundTransform= snd;
			}
		}
		
		public function setBufor(x:Number):void {
			bufor = x;
			if (ns) {
				ns.bufferTime = x;
			}
			
		}
		
		public function getBuffor():Number {
			return bufor;
		}
		
		public function getBufforLength():Number {
			if (ns) {
				return ns.bufferLength;
			} else {
				return -1;
			}
		}
		
		public function get currentBytesPerSecond():int {
			if (nc.connected && ns) {
				return ns.info.currentBytesPerSecond;
			} else {
				return -1;
			}			
		}

		
		public function toString():String {
			return "NetStream:";
		}
		
		
		
		
	}
}